package com.shopping.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.shopping.commons.response.DataGradResp;
import com.shopping.commons.response.MessageResp;
import com.shopping.commons.util.CommonCode;
import com.shopping.mapper.CommodityMapper;
import com.shopping.model.Commodity;
import com.shopping.param.CommodityParam;
import com.shopping.response.CommodityResp;
import com.shopping.service.ICommodityService;

@Service
public class CommodityService implements ICommodityService{

	@Autowired
	private CommodityMapper commodityMapper;
	
	private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public MessageResp insert(Commodity param) {
		MessageResp resp=new MessageResp();
		//TODO:   这里要对param进行一次加工，添加诸如  addrid    updateperson updatetime  delflag 
		//status     这样的一些参数
		Random r=new Random();
		param.setCommid(r.nextInt(10000)+100000);//   100000-199999  之间的随机数
		param.setDelflag(CommonCode.DELFLAG_NORMAL);
		param.setCreatedate(sdf.format(new Date()));
		if(commodityMapper.insertSelective(param)>0){
			//成功了
			resp.setCode("0000");
			resp.setMsg("添加成功");
		}else{
			resp.setCode("9999");
			resp.setMsg("添加失败");
		}
		return resp;
	}

	public MessageResp update(Commodity param) {
		MessageResp resp=new MessageResp();
		Commodity entity=commodityMapper.selectByPrimaryKey(param.getCommid());
		if(entity==null){
			//没找到
			resp.setCode("9999");
			resp.setMsg("未找到修改项");
			return resp;
		}
//		entity.setDelflag("1");
//		entity.setUpdatedate(sdf.format(new Date()));
		entity.setCname(param.getCname());
		entity.setDelflag(param.getDelflag()==null?CommonCode.DELFLAG_NORMAL:param.getDelflag());
		entity.setCdescribe(param.getCdescribe());
		entity.setStatus(param.getStatus()==null?CommonCode.STATUS_ONE:param.getStatus());
		entity.setCimg(param.getCimg());
		entity.setPrice(param.getPrice());
		entity.setStock(param.getStock());
		
		//自动更新   更新的时间  和更新的人
		entity.setUpdatedate(sdf.format(new Date()));
//		if(addressMapper.updateByPrimaryKey(param)>0){  hH会更新掉所有的字段，没有值的就用null覆盖
		if(commodityMapper.updateByPrimaryKeySelective(entity)>0){
			//成功了
			resp.setCode("0000");
			resp.setMsg("修改成功");
		}else{
			resp.setCode("9999");
			resp.setMsg("修改失败");
		}
		return resp;
	}

	public MessageResp delete(Integer id) {
		//  在实现类中写的任何   与 接口无关的方法，都是在Controller中 不可见的
		MessageResp resp=new MessageResp();
		//TODO:如果没有这个id怎么办
		Commodity entity=commodityMapper.selectByPrimaryKey(id);
		if(entity==null){
			//没找到
			resp.setCode("9999");
			resp.setMsg("未找到删除项");
			return resp;
		}
		entity.setDelflag(CommonCode.DELFLAG_DELETE);
		entity.setUpdatedate(sdf.format(new Date()));//asf.pares("2015-10-10 10:10:10")
//		try {
//			Integer.parseInt("1asdfa");
//		} catch (NumberFormatException e) {
//			e.printStackTrace();
//		}
		if(commodityMapper.updateByPrimaryKeySelective(entity)>0){
			//成功了
			resp.setCode("0000");
			resp.setMsg("删除成功");
		}else{
			resp.setCode("9999");
			resp.setMsg("删除失败");
		}
		return resp;
	}

	public DataGradResp<CommodityResp> select(CommodityParam param) {
		DataGradResp<CommodityResp> resp=new DataGradResp<CommodityResp>();//
		//查询     可能有多个，可能是单个
		//如果前端没有传任何参数的话，delflag也要特殊处理下
		param.setDelflag(param.getDelflag()==null?"0":param.getDelflag());
		List<CommodityResp> commodityList;
		try {
			commodityList = commodityMapper.select(param);
		} catch (Exception e) {
			e.printStackTrace();
			resp.setCode("9999");
			resp.setMsg("查询失败");
			return resp;
		}
		resp.setCode("0000");
		resp.setMsg("查询成功");
		resp.setRows(commodityList);
		//resp.setObj(commodityList);
		resp.setTotal(commodityMapper.count(CommonCode.DELFLAG_NORMAL));
		return resp;
	}

	public DataGradResp<Commodity> select(Commodity param) {
		// TODO Auto-generated method stub
		return null;
	}

	public MessageResp batchDelete(String ids) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<CommodityResp> select(String method, CommodityParam param) {
		if(StringUtils.isEmpty(method)||"default".equals(method)){
			return commodityMapper.select(param);
		}else{
			return commodityMapper.select(param);
		}
	}

	public List<CommodityResp> findByIds(String ids) {
		String [] id_=null;
		try {
			id_=ids.split(",");
		} catch (Exception e) {
			return new ArrayList<CommodityResp>();
		}
		return commodityMapper.selectById(id_);
	}
}
